﻿package com.petermacko.utils {
	import com.petermacko.utils.Singleton;		

	/**
	 * The DescribeType classs wraps the flash.util.describeType
	 * function and caches all object descriptions so that
	 * describeType will only be called once for any object.
	 */
	final public class DescribeTypeCache 
	{
		private static var inst:DescribeTypeCache; //Singleton instance.
		private var dcache:Cache; //The type cache.
		
		//Singleton access.
		public static function gi():DescribeTypeCache {
			if(!inst) inst = Singleton.gi(DescribeTypeCache);
			return inst;
		}
		
		public function DescrybeTypeCache():void {
			Singleton.assertSingle(DescribeTypeCache);
			dcache = new Cache();
		}
		
		//Returns the type description for an object.
		public function describeType(obj:*):XML {
			var x:XML;
			if(dcache.isCached(obj)) {
				x = dcache.getCachedObject(obj);
			} else {
				x = describeType(obj);
				dcache.cacheObject(obj,x);				
			}
			return x;
		}
	}
}